import {
	Config
} from "./Config.js";

class Base {
	constructor() {
		this.url = Config.api;
		this.url1 = Config.api1;
		this.imgurl = Config.imgapi
	}


	// wx微信支付
	wxPay(data) {
		return new Promise((resolve, reject) => {
			WeixinJSBridge.invoke('getBrandWCPayRequest', {
				"paySign": data.paySign,
				"appId": data.appId,
				"nonceStr": data.nonceStr,
				"package": data.wpackage,
				"signType": data.signType,
				"timeStamp": data.timeStamp
			}, (res) => {
				resolve(res)
			});
		})
	}

	//接口请求,promise版 url接口 data 需要想后端传的数据,对象格式  header头穿传的字段 openId ,也可增加字段  用户端接口
	request(url, method, data, header) {
		return new Promise((resolve, reject) => {

			if (!header) {
				header = {
					'content-type': 'application/x-www-form-urlencoded'
				}
			}

			const userToken = uni.getStorageSync('userToken');
			if (userToken) {
				header["Authorization"] = "Bearer " + userToken;
			} else {
				console.log("获取不到userToken")

			}
			uni.request({
				url: this.url + url,
				method: method,
				data: data,
				dataType: "json",
				header: header,
				timeout: 15000,
				success: (res) => {
					if (res.data.code == '70004' || res.data.code == '70006' || res.data.code == '20001') {
						//无效token
						uni.clearStorageSync();
						uni.showModal({
						    title: '提示',
						    content: '登录超时，请退出重新登录',
							showCancel: false,
						    success: function (res) {
						        if (res.confirm) {
						            uni.reLaunch({
						            	url: '/pages/login/login'
						            })
						        }
						    }
						});
					}
					resolve(res)

				},
				fail(res) {

					reject(res)

				}
			});

		})

	}

	//接口请求,promise版 url接口 data 需要想后端传的数据,对象格式  header头穿传的字段 openId ,也可增加字段   运营商端接口
	request1(url, method, data, header) {
		return new Promise((resolve, reject) => {
			if (!header) {
				header = {
					'content-type': 'application/x-www-form-urlencoded'
				}
			}
			const userToken = uni.getStorageSync('userToken');
			if (userToken) {
				header["Authorization"] = "Bearer " + userToken;
			} else {
				console.log("获取不到userToken")

			}
			uni.request({
				url: this.url1 + url,
				method: method,
				data: data,
				dataType: "json",
				header: header,
				timeout: 15000,
				success: (res) => {
					if (res.data.code == '70004') {
						//token已过期
						uni.removeStorageSync('userToken');
						this.renewalJWT(url, method, data, header, 2);
						
					}
					else if (res.data.code == '70006' || res.data.code == '20001') {
						//无效token
						uni.clearStorageSync();
						uni.showModal({
						    title: '提示',
						    content: '登录超时，请退出重新登录',
							showCancel: false,
						    success: function (res) {
						        if (res.confirm) {
						            uni.reLaunch({
						            	url: '/pages/login/login'
						            })
						        }
						    }
						});
					}
					resolve(res)

				},
				fail(res) {

					reject(res)

				}
			});
		})
	}

	// 验证是否登陆过
	isLogin() {
		if (uni.getStorageSync('isLogin') == 1) {
			return true;
		} else {
			return false
		}
	}


	//前往登录页面
	loginPage() {
		uni.navigateTo({
			url: "/pages/login/login"
		})
	}

	//弹出提示窗方法 参数说明arg1:title标题提示 arg2:提示的详情的内容 cb1:成功的回调  cb2:失败的回调
	prompt(arg1, arg2, cb1, cb2) {
		uni.showModal({
			title: arg1,
			content: arg2,
			success: function(res) {
				if (res.confirm) {
					cb1()
				} else if (res.cancel) {
					cb2()
				}
			}
		});
	}

	// 操作成功, 弹出的提示框,过两秒会自动消失的successPrompt缩写
	sPrompt(arg) {
		uni.showToast({
			title: arg
		})
	}

	promptInfo(arg) {
		uni.showToast({
			title: '小主,没有更多数据了',
			icon: 'none'
		})
	}

	// 操作成功,过两秒会自动消失的successPrompt缩写
	fPrompt(arg) {
		uni.showToast({
			title: arg,
			icon: 'none'
		})
	}


	// 校验手机号
	checkPhone(e) {
		if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(e))) {
			uni.showToast({
				title: "请输入正确的手机号",
				icon: "none"
			})
			return false;
		} else {
			return true
		}
	}

	// 开启等待
	loading(obj) {
		uni.showLoading({
			title: obj.title,
			mask: obj.mask
		});
	}

	// 关闭等待
	hideLoading() {
		uni.hideLoading();
	}
	
	//协议
	agreement(data) {
		return new Promise((resolve, reject) => {
			this.request('wxlogin/agreement', 'GET', data)
				.then(res => {
					resolve(res)
				})
				.catch(err => {
					reject(err)
				})
		})
	}
	
	//判断登录和续签JWT, type=1是request,type=2是request1
	renewalJWT(url2, method2, data2, header2, type){
		let that = this;
		var header = {
			'content-type': 'application/x-www-form-urlencoded'
		}
		const userId = uni.getStorageSync('userId');
		var data = {
			'userId': userId
		}
		uni.request({
			url: that.url + 'wxlogin/renewalJWT',
			method: 'POST',
			data: data,
			dataType: "json",
			header: header,
			timeout: 15000,
			success: (res) => {
				console.log(res);
				if(res.data.code == 200){
					uni.setStorageSync('userToken', res.data.data.token);
					if(type == 2){
						that.request1(url2, method2, data2, header2);
					}
					else{
						that.request(url2, method2, data2, header2);
					}
				}
			}
		});
	}
	

}
export {
	Base
}